﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Model;


namespace MsDAL
{
   public partial  class Bas1a40Base
    {		
		/// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual  SqlApp Add(Model.Bas1a40 bas1a40)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Bas1a40(");
			strSql.Append("cntcode ,startdate ,endate ,slistid ,comcode ,comname ,lvlistid ,listid ,alistid ,splistid ,paycycle ,paydate ,backmk ,mostamt ,foregift ,status ,createdate ,lastamt ,muser ,way1 ,way1key1 ,way1key2 ,way2 ,way2key1 ,way2key2 )");
			strSql.Append(" values (");
			strSql.Append("@cntcode ,@startdate ,@endate ,@slistid ,@comcode ,@comname ,@lvlistid ,@listid ,@alistid ,@splistid ,@paycycle ,@paydate ,@backmk ,@mostamt ,@foregift ,@status ,@createdate ,@lastamt ,@muser ,@way1 ,@way1key1 ,@way1key2 ,@way2 ,@way2key1 ,@way2key2 )");		

			SqlParameter[] parameters = {
				new SqlParameter("@cntcode",SqlDbType.NVarChar),
				new SqlParameter("@startdate",SqlDbType.DateTime),
				new SqlParameter("@endate",SqlDbType.DateTime),
				new SqlParameter("@slistid",SqlDbType.NVarChar),
				new SqlParameter("@comcode",SqlDbType.NVarChar),
				new SqlParameter("@comname",SqlDbType.NVarChar),
				new SqlParameter("@lvlistid",SqlDbType.NVarChar),
				new SqlParameter("@listid",SqlDbType.NVarChar),
				new SqlParameter("@alistid",SqlDbType.NVarChar),
				new SqlParameter("@splistid",SqlDbType.NVarChar),
				new SqlParameter("@paycycle",SqlDbType.Int),
				new SqlParameter("@paydate",SqlDbType.DateTime),
				new SqlParameter("@backmk",SqlDbType.NVarChar),
				new SqlParameter("@mostamt",SqlDbType.Decimal),
				new SqlParameter("@foregift",SqlDbType.Decimal),
				new SqlParameter("@status",SqlDbType.Int),
				new SqlParameter("@createdate",SqlDbType.DateTime),
				new SqlParameter("@lastamt",SqlDbType.Decimal),
				new SqlParameter("@muser",SqlDbType.NVarChar),
				new SqlParameter("@way1",SqlDbType.NVarChar),
				new SqlParameter("@way1key1",SqlDbType.NVarChar),
				new SqlParameter("@way1key2",SqlDbType.NVarChar),
				new SqlParameter("@way2",SqlDbType.NVarChar),
				new SqlParameter("@way2key1",SqlDbType.NVarChar),
				new SqlParameter("@way2key2",SqlDbType.NVarChar)
				};
			parameters[0].Value = bas1a40.Cntcode;
			parameters[1].Value = bas1a40.Startdate;
			parameters[2].Value = bas1a40.Endate;
			parameters[3].Value = bas1a40.Slistid;
			parameters[4].Value = bas1a40.Comcode;
			parameters[5].Value = bas1a40.Comname;
			parameters[6].Value = bas1a40.Lvlistid;
			parameters[7].Value = bas1a40.Listid;
			parameters[8].Value = bas1a40.Alistid;
			parameters[9].Value = bas1a40.Splistid;
			parameters[10].Value = bas1a40.Paycycle;
			parameters[11].Value = bas1a40.Paydate;
			parameters[12].Value = bas1a40.Backmk;
			parameters[13].Value = bas1a40.Mostamt;
			parameters[14].Value = bas1a40.Foregift;
			parameters[15].Value = bas1a40.Status;
			parameters[16].Value = bas1a40.Createdate;
			parameters[17].Value = bas1a40.Lastamt;
			parameters[18].Value = bas1a40.Muser;
			parameters[19].Value = bas1a40.Way1;
			parameters[20].Value = bas1a40.Way1key1;
			parameters[21].Value = bas1a40.Way1key2;
			parameters[22].Value = bas1a40.Way2;
			parameters[23].Value = bas1a40.Way2key1;
			parameters[24].Value = bas1a40.Way2key2;
			
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");
        }
	
        /// <summary>
        /// 判断某个值是否存在表中的某个字段
        /// </summary>
        /// <param name="Condition">条件字段名 例:A=@A</param>
        /// <param name="hs">参数和值 如hs["@A"]=123</param>
        /// <returns></returns>
        public virtual bool Exists(String condition, Hashtable hs)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(1) from Bas1a40 ");
			if (!string.IsNullOrEmpty(condition))
				strSql.Append(" where " + condition);
			List<SqlParameter> list = DBHelper.GetListByHashtable(hs);
			if (list.Count > 0)
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString(), list.ToArray())) > 0;
			else
				return Convert.ToInt32(DBHelper.ExecuteScalar(strSql.ToString())) > 0;
        }

		/// <summary>
        /// 更新一条数据
        /// </summary>
        public virtual SqlApp Update(Model.Bas1a40 bas1a40)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Bas1a40 set ");
			strSql.Append("cntcode=@cntcode ,startdate=@startdate ,endate=@endate ,slistid=@slistid ,comcode=@comcode ,comname=@comname ,lvlistid=@lvlistid ,alistid=@alistid ,splistid=@splistid ,paycycle=@paycycle ,paydate=@paydate ,backmk=@backmk ,mostamt=@mostamt ,foregift=@foregift ,status=@status ,createdate=@createdate ,lastamt=@lastamt ,muser=@muser ,way1=@way1 ,way1key1=@way1key1 ,way1key2=@way1key2 ,way2=@way2 ,way2key1=@way2key1 ,way2key2=@way2key2 ");
			strSql.Append("where listid=@listid ");
			SqlParameter[] parameters = {
				new SqlParameter("@cntcode", SqlDbType.NVarChar),
				new SqlParameter("@startdate", SqlDbType.DateTime),
				new SqlParameter("@endate", SqlDbType.DateTime),
				new SqlParameter("@slistid", SqlDbType.NVarChar),
				new SqlParameter("@comcode", SqlDbType.NVarChar),
				new SqlParameter("@comname", SqlDbType.NVarChar),
				new SqlParameter("@lvlistid", SqlDbType.NVarChar),
				new SqlParameter("@listid", SqlDbType.NVarChar),
				new SqlParameter("@alistid", SqlDbType.NVarChar),
				new SqlParameter("@splistid", SqlDbType.NVarChar),
				new SqlParameter("@paycycle", SqlDbType.Int),
				new SqlParameter("@paydate", SqlDbType.DateTime),
				new SqlParameter("@backmk", SqlDbType.NVarChar),
				new SqlParameter("@mostamt", SqlDbType.Decimal),
				new SqlParameter("@foregift", SqlDbType.Decimal),
				new SqlParameter("@status", SqlDbType.Int),
				new SqlParameter("@createdate", SqlDbType.DateTime),
				new SqlParameter("@lastamt", SqlDbType.Decimal),
				new SqlParameter("@muser", SqlDbType.NVarChar),
				new SqlParameter("@way1", SqlDbType.NVarChar),
				new SqlParameter("@way1key1", SqlDbType.NVarChar),
				new SqlParameter("@way1key2", SqlDbType.NVarChar),
				new SqlParameter("@way2", SqlDbType.NVarChar),
				new SqlParameter("@way2key1", SqlDbType.NVarChar),
				new SqlParameter("@way2key2", SqlDbType.NVarChar)
				};
			parameters[0].Value = bas1a40.Cntcode;
			parameters[1].Value = bas1a40.Startdate;
			parameters[2].Value = bas1a40.Endate;
			parameters[3].Value = bas1a40.Slistid;
			parameters[4].Value = bas1a40.Comcode;
			parameters[5].Value = bas1a40.Comname;
			parameters[6].Value = bas1a40.Lvlistid;
			parameters[7].Value = bas1a40.Listid;
			parameters[8].Value = bas1a40.Alistid;
			parameters[9].Value = bas1a40.Splistid;
			parameters[10].Value = bas1a40.Paycycle;
			parameters[11].Value = bas1a40.Paydate;
			parameters[12].Value = bas1a40.Backmk;
			parameters[13].Value = bas1a40.Mostamt;
			parameters[14].Value = bas1a40.Foregift;
			parameters[15].Value = bas1a40.Status;
			parameters[16].Value = bas1a40.Createdate;
			parameters[17].Value = bas1a40.Lastamt;
			parameters[18].Value = bas1a40.Muser;
			parameters[19].Value = bas1a40.Way1;
			parameters[20].Value = bas1a40.Way1key1;
			parameters[21].Value = bas1a40.Way1key2;
			parameters[22].Value = bas1a40.Way2;
			parameters[23].Value = bas1a40.Way2key1;
			parameters[24].Value = bas1a40.Way2key2;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");			
		}
		
		public virtual SqlApp Delete(Model.Bas1a40 bas1a40)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("DELETE FROM  Bas1a40  ");
			strSql.Append("WHERE listid=@listid ");
			SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
			parameters[0].Value = bas1a40.Listid;
	
			return new SqlApp(strSql.ToString(),new ArrayList(parameters), false, "数据保存失败！");		
		}
		
		/// <summary>
        /// 根据Listid得到整个 Bas1a40对象,如果没查到，返回null
        /// </summary>
        /// <param name="Listid">该实体主键 Listid</param>
        /// <returns>实体Bas1a40的对象</returns>
		public virtual Model.Bas1a40 GetModelById(string Listid)
        {
			string where = "listid=@listid ";
			
			string sql = GetSql(where,null);
			
			List<SqlParameter> parameters = new List<SqlParameter>();
			
			SqlParameter p0 = new SqlParameter("@Listid",SqlDbType.NVarChar);
			p0.Value = Listid;
			parameters.Add(p0);
			
			DataTable dt = DBHelper.GetData(sql, parameters.ToArray());
			
			//调用实体Bas1a40的GetBas1a40ListByDataTable方法得到实体对象
			List<Model.Bas1a40> list =Model.Bas1a40.GetBas1a40ListByDataTable(dt);
			
			return  (list.Count>0) ? list[0] : null;
        }
		
		
		 /// <summary>
        /// 得到Sql语句
        /// </summary>
        /// <param name="Condition">Sql语句的条件</param>
		/// <param name="OrderBy">排序条件，默认为String.Empty</param>
        /// <returns></returns>
        protected virtual String GetSql(string condition,string orderBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [cntcode] ,[startdate] ,[endate] ,[slistid] ,[comcode] ,[comname] ,[lvlistid] ,[listid] ,[alistid] ,[splistid] ,[paycycle] ,[paydate] ,[backmk] ,[mostamt] ,[foregift] ,[status] ,[createdate] ,[lastamt] ,[muser] ,[way1] ,[way1key1] ,[way1key2] ,[way2] ,[way2key1] ,[way2key2] ");
            strSql.Append(" FROM Bas1a40 ");
 			
			if (!string.IsNullOrEmpty(condition)) strSql.Append(" where " + condition);				
			if(!string.IsNullOrEmpty(orderBy)) strSql.Append( " order by " + orderBy );
			
            return  strSql.ToString();
        }

        /// <summary>
        /// 根据条件和参数得到Bas1a40对象
        /// </summary>
        /// <param name="Condition">Sql条件</param>
        /// <param name="hs">参数和值</param>
        /// <returns></returns>
        public virtual Model.Bas1a40 GetModel(String Condition, Hashtable hs)
        {
			String sql = GetSql(Condition,String.Empty);
			DataTable dt = DBHelper.GetTableByCondition(sql,  hs);
			return (dt.Rows.Count > 0) ? Model.Bas1a40.GetBas1a40ListByDataTable(dt)[0] : null;
        }

        /// <summary>
        /// 根据主键的值和某字段得到其值,可以用来判断是否存在
        /// </summary>
        /// <param name="TableKeyValue">主键值</param>
        /// <param name="FieldName">要查询的字段名称</param>
        /// <returns></returns>
        public virtual Object GetValueByField(Object TableKeyValue, Object FieldName)
        {
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select " + FieldName + " from Bas1a40 ");
			strSql.Append(" where listid=@TableKeyValue ");
			SqlParameter p = new SqlParameter("@TableKeyValue", TableKeyValue);

			return DBHelper.ExecuteScalar(strSql.ToString(), p);
        }
		
        /// <summary>
        /// 分页SQL
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual ReturnPageData GetList(Pager page)
        {
            ReturnPageData data = new ReturnPageData();
            //page.PageIndex++;
            page.PrimaryKey = "listid ";

            page.ShowFields = " [cntcode] ,[startdate] ,[endate] ,[slistid] ,[comcode] ,[comname] ,[lvlistid] ,[listid] ,[alistid] ,[splistid] ,[paycycle] ,[paydate] ,[backmk] ,[mostamt] ,[foregift] ,[status] ,[createdate] ,[lastamt] ,[muser] ,[way1] ,[way1key1] ,[way1key2] ,[way2] ,[way2key1] ,[way2key2] ";

            page.TableName = " Bas1a40 ";

			page.StrWhere = string.IsNullOrEmpty(page.StrWhere) ? " 1=1 " : page.StrWhere;
			
            page.StrWhere = string.IsNullOrEmpty(page.AutoWhere) ? page.StrWhere : page.StrWhere + " And ("+ page.AutoWhere +")";

            page.For = PagerFor.Count;
            DataTable dtRowCount = DBHelper.GetSelectLimit(page);
            if (dtRowCount == null || dtRowCount.Rows.Count == 0)
            {
                data.AllRowCount = 0;
                return data; //NULL DATA
            }
            data.AllRowCount = Convert.ToInt32(dtRowCount.Rows[0]["Total"]);
            page.For = PagerFor.Data;

            DataTable dtPageData = DBHelper.GetSelectLimit(page);
            data.CurrnetPageData = dtPageData;

            //汇总
            page.For = PagerFor.Summary;
            data.SummaryData = DBHelper.GetSelectLimit(page);
            
            return data;
        }		
		
		public virtual DataTable GetList(string c,Hashtable hs)
		{
			string sql = GetSql(c,null); //得到SQL语句
			//返回结果集
			return DBHelper.GetData(sql,DBHelper.GetListByHashtable(hs).ToArray());
		}
    }
}
